<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" plain>搜索</el-button>
        <el-button type="info" icon="el-icon-refresh-left" size="mini" @click="resetQuery" plain>重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:addressBook:add']"
          plain>新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:addressBook:edit']"
          plain>编辑
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:addressBook:remove']"
          plain>删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center">
        <template scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编号" v-if="false" align="center" prop="bookId"/>
      <el-table-column
        label="姓名"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <!--      <el-table-column-->
      <!--        label="固定电话"-->
      <!--        align="center"-->
      <!--        prop="telephone"-->
      <!--      />-->
      <el-table-column
        label="移动电话"
        align="center"
        prop="mobilePhone"
      />
      <el-table-column
        label="邮件地址"
        align="center"
        prop="email"
      />
      <!--      <el-table-column-->
      <!--        label="工作单位"-->
      <!--        align="center"-->
      <!--        prop="unit"-->
      <!--      />-->
      <el-table-column
        label="家庭住址"
        align="center"
        prop="address"
      />
      <el-table-column label="创建者" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:addressBook:edit']"
            plain>编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:addressBook:remove']"
            plain>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通讯录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-card class="box-card">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" placeholder="请输入姓名"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="固定电话" prop="telephone">
                <el-input v-model="form.telephone" placeholder="请输入固定电话"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="手机号" prop="mobilePhone">
                <el-input v-model="form.mobilePhone" placeholder="请输入手机号"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮件地址" prop="email">
                <el-input v-model="form.email" placeholder="请输入邮件地址"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工作单位" prop="unit">
                <el-input v-model="form.unit" placeholder="请输入工作单位"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="家庭住址" prop="address">
                <el-input v-model="form.address" placeholder="请输入家庭住址"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" icon="el-icon-success" @click="submitForm">确 定</el-button>
        <el-button type="danger" icon="el-icon-error" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import BasePage from "@/components/Base/BasePage";

export default {
  name: "AddressBook",
  extends: BasePage,
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        address: [
          {required: true, message: "家庭住址不能为空", trigger: "blur"}
        ],
        email: [
          {required: true, message: "邮箱地址不能为空", trigger: "blur"},
          {type: "email", message: "'请输入正确的邮箱地址", trigger: ["blur", "change"]}
        ],
        telephone: [
          {validator: this.checkTelPhone, trigger: 'blur'}
        ],
        mobilePhone: [
          {required: true, message: "手机号码不能为空", trigger: "blur"},
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询通讯录列表 */
    getList() {
      this.loading = true;
      const url = '/system/address/book/list';
      this.getDataList(url, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //固定电话格式验证
    checkTelPhone(rule, value, callback) {
      const reg = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,4}))?$/;
      if (value != null && value != "" && !reg.test(value)) {
        return callback(new Error('请输入正确的座机号'))
      }
      return callback()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookId: undefined,
        name: undefined,
        telephone: undefined,
        mobilePhone: undefined,
        email: undefined,
        unit: undefined,
        address: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通讯录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bookId = row.bookId || this.ids;
      const url = '/system/address/book/' + bookId;
      this.getData(url).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改通讯录";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.open = false;
          const url = '/system/address/book';
          if (this.form.bookId != undefined) {
            this.updateData(url, this.form).then(response => {
              this.msgSuccess("编辑成功");
              this.getList();
            });
          } else {
            this.addData(url, this.form).then(response => {
              this.msgSuccess("保存成功");
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let _this = this;
      const ids = row.bookId || this.ids;
      _this.$confirm('是否确认要删除数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        const url = '/system/address/book/' + ids;
        _this.delData(url).then(() => {
          _this.getList();
          _this.msgSuccess("删除成功");
        });
      });
    }
  }
};
</script>
